/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import me.peng.domain.Smsapi;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.SmsapiRepository;
import me.peng.service.SmsapiService;
import me.peng.service.dto.SmsapiDto;
import me.peng.service.dto.SmsapiQueryCriteria;
import me.peng.service.mapstruct.SmsapiMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-10-13
**/
@Service
@RequiredArgsConstructor
public class SmsapiServiceImpl implements SmsapiService {

    private final SmsapiRepository smsapiRepository;
    private final SmsapiMapper smsapiMapper;

    @Override
    public Map<String,Object> queryAll(SmsapiQueryCriteria criteria, Pageable pageable){
        Page<Smsapi> page = smsapiRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(smsapiMapper::toDto));
    }

    @Override
    public List<SmsapiDto> queryAll(SmsapiQueryCriteria criteria){
        return smsapiMapper.toDto(smsapiRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SmsapiDto findById(Integer id) {
        Smsapi smsapi = smsapiRepository.findById(id).orElseGet(Smsapi::new);
        ValidationUtil.isNull(smsapi.getId(),"Smsapi","id",id);
        return smsapiMapper.toDto(smsapi);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SmsapiDto create(Smsapi resources) {
        return smsapiMapper.toDto(smsapiRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Smsapi resources) {
        Smsapi smsapi = smsapiRepository.findById(resources.getId()).orElseGet(Smsapi::new);
        ValidationUtil.isNull( smsapi.getId(),"Smsapi","id",resources.getId());
        smsapi.copy(resources);
        smsapiRepository.save(smsapi);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            smsapiRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SmsapiDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SmsapiDto smsapi : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("请求地址", smsapi.getUrl());
            map.put("账户名", smsapi.getAcount());
            map.put("密钥", smsapi.getSecretkey());
            map.put("模板id", smsapi.getTemplate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
